﻿
import YouHP.Graphics.*;

class MultiTreatment extends MultiChartBase
{
	//图表全局数据
	var mGroupCount : Number = 1;		//图表的组数
	var mGroup : Array = new Array();	//图表的组信息
	
	//图例数据部分变量
	var mLegendOption : Array = new Array();	//图例数据选项
	
	//其它
	var mGap : Number = 15;						//图表与图例X轴上间隔
	var mTitleColor : Number = 0xff0000;		//图标标题文字颜色
		
	//坐标轴相关数据  
	var mYGap : Number = 30;					//Y轴上的最小间隔
	var mChartTop : Number = 60;				//图表数据顶部的Y轴偏移
	var mChartLeft : Number = mLegendLeft + mLegendWidth + mGap;		//X轴的偏移
	
	//治疗名称
	var mTreatmentArr : Array = new Array();
	
    function MultiTreatment(lv : Object)
    {
        mClip = _root.createEmptyMovieClip("MultiTreatment", _root.getNextHighestDepth());
        mData = lv;		
        
		//提取治疗图表公用信息
        ParseBase(lv); 
		//提取症状图表所需要的私有信息
        ParsePrivateData();
		
		//设置图例数据
		SetLegendData();
		
		//画图
        Draw();
	}
	
	private function ParsePrivateData()
	{
		var tmpArray : Array;
		var cnt;
		
		//解析组与每组中治疗个数
		mGroupCount = Number(mData["GroupCount"]);
		for(var i=1; i <= mGroupCount; i++)
        {
			tmpArray = mData["Group"+i].split(";");
			cnt = Number(tmpArray[1]);
			mGroup.push([tmpArray[0], cnt]);
			
			//解析治疗名称
			for(var j=1; j<=cnt; j++)
			{
				tmpArray = mData["Var_" + i + "_" + j].split(";");
				mTreatmentArr.push([tmpArray[0], tmpArray[1]]);
			}
		}
	}
	
	private function SetLegendData()
	{
		//数组中内容分别代表：治疗目的， 治疗目的表示颜色， 表示的矩形宽度
        mLegendOption.push(["治疗本病", 0xACD6FF, 10]);
        mLegendOption.push(["治疗症状", 0x3270CF, 10]);
        mLegendOption.push(["医疗保健", 0xD4D4D4, 10]);
        mLegendOption.push(["其它", 0x000040, 10]);		
	}
	
    private function Draw()
    {
		//画图例
        DrawLegend();
		
		//画网格线以及坐标值
		DrawGrid();

		//画图表数据
        DrawData();        
    }	
	
    public function DrawLegend()
    {
      	var mc : MovieClip = mClip;
        var gs:Graphics = new Graphics(mClip);

        var pen = new Pen(1.0,0x000000,100);
		var titlePen = new Pen(1.0,0xcccccc,100);
		var brush = new SolidBrush(0x000000, 100);
        var startx = mLegendLeft + mLegendXGap;
        var starty = 0;
        var y = 0;
		var curLegendTop = mLegendTop;
		
		for(var k=0; k < mGroupCount; k++)
	   	{
			starty = curLegendTop;
			gs.DrawLine(titlePen, mLegendLeft, curLegendTop, mLegendWidth, curLegendTop, 1, 1); 
			//DrawHtmlText("<b>" + mGroup[k][0] + "</b>", startx, starty, mTitleColor, 20);
			DrawHtmlText("<b>" + mGroup[k][0] + "</b>", startx, starty, mTitleColor, 20);
			
			if(k == 0)	//只对第一组数据添加legend说明
			{ 
				starty += 2*mLegendYGap;
        		for(var i=0; i < mLegendOption.length; i++)
        		{
            		y = starty + i * mLegendYGap;
            
            		brush.setColor(mLegendOption[i][1]);
            		gs.FillRect(brush, startx + 10, y, 12, 12);
            		gs.DrawRect(pen, startx + 10, y, 12, 12);
            
           	 		var label:TextField = mc.createTextField("option_" + i ,mc.getNextHighestDepth(),
              			startx + 25, y-3, 0, 0);
            		var myformat = new TextFormat();
        			myformat.color = 0xff0000;
		    		myformat.align = "left";
		    		label.setTextFormat(myformat);
		            
        			label.html = true;
		    		label.autoSize = "left";
		    		label.htmlText = mLegendOption[i][0];
        		}
						
				gs.DrawRect(pen, startx, starty - mLegendYGap/2, mLegendWidth - 4*mLegendXGap, (mLegendOption.length + 0.5) *mLegendYGap);
			}			

			curLegendTop = curLegendTop + mYGap * Number(mGroup[k][1] + 2.5);	//计算下一个图表标题显示的纵向位置			
			//判断是否会重叠在图例上，如果是，则需要调整当前的图例顶部
			if(curLegendTop <= starty + mLegendOption.length*mLegendYGap)
			{
				curLegendTop = starty + (mLegendOption.length+0.5)*mLegendYGap
			}
		}        
    }
	
    public function DrawGrid()
    {
       	var mc : MovieClip = mClip;
        var gs:Graphics = new Graphics(mClip);		
		var gridPen = new Pen(1.0,0xcccccc,100);
		var yTickCnt : Number = 0;
		var xTickCnt : Number = 0;
		var mXGap : Number = 40 ;					
		var tmpY : Number = mChartTop;
		var tmpX : Number = mChartLeft;
		var curGroupTop : Number = mChartTop ;
		var tmpXVal : String ;
		var varArr : Array;
		var textGap = 10;
		var curTreatmentIdx=0;
		
		//解析X轴坐标
		ParseXCoordinate(mDosageDate, mUpdateDate, mShowMode) ;		
		
       	for(var i=0; i < mGroupCount; i++)
	   	{
			yTickCnt = Number(mGroup[i][1]);
			
			//画网格的矩形边框
			gs.DrawRect(gridPen, mChartLeft, curGroupTop, mChartWidth, mYGap*(yTickCnt + 1));
			
			tmpY = curGroupTop + mYGap;
			for(var j=0; j < yTickCnt; j++)	
			{
				//画横向的网格线
				gs.DrawDashLine(gridPen, mChartLeft, tmpY, mChartLeft + mChartWidth, tmpY, 1, 1);
				//画治疗名称(Y轴坐标值)
				DrawHtmlText(mTreatmentArr[curTreatmentIdx++][0], mChartLeft + mChartWidth + textGap, tmpY - textGap, mTitleColor, 8);
				
				tmpY += mYGap;
			}			
						
			curGroupTop = tmpY + mYGap ;
	   	}
		
		//画纵横向的网格线
		xTickCnt = mChartXCount;
		for(var k=1; k < xTickCnt; k++)
		{
			gs.DrawDashLine(gridPen, mChartLeft + k*mChartXGap, mChartTop, mChartLeft + k*mChartXGap, tmpY, 1, 1); 
			tmpX += mXGap;				
		}	
		
		//画X轴坐标
		for(i=0; i<mChartCoordinate.length; i++)
		{		
			tmpXVal = Utils.DateToXStr(mChartCoordinate[i][0]);
			tmpX = mChartCoordinate[i][1];
			
			DrawHtmlText(tmpXVal, mChartLeft + tmpX - mChartXValidPos, tmpY + 0.5*mYGap, mTitleColor, 8);
		}
		
		//画最新更新点的标志线
		var tmpWidth = 1;
		var tmpColor = 0xA0A000;
		gridPen.setColor(tmpColor);
		gs.DrawRect(gridPen, mChartLeft+mChartWidth-tmpWidth/2.0, mLegendTop, tmpWidth, tmpY-mLegendTop);
		DrawHtmlText("<b>最近更新点</b>", mChartLeft+mChartWidth, mChartTop/2.0-textGap, tmpColor, 8);		
    }
	
	private function DrawPoint(pen:Pen, brush:SolidBrush, x:Number, y:Number, width:Number, height:Number, value:String, pointCnt:Number)
	{
		var mc:MovieClip = _root.createEmptyMovieClip( "Tip_" + pointCnt, _root.getNextHighestDepth() );
		var gs:Graphics = new Graphics(mc);		
		var link:String = "" ;	//对应的超链接
		
		mc.ToolTip(value, link);
	
		gs.DrawRect(pen, x, y, width, height);
		gs.FillRect(brush, x, y, width, height);		
		
		this.mChildmcArr.push(mc);
	}	
	
	private function DrawRectWithTips(pen:Pen, brush:SolidBrush, x:Number, y:Number, width:Number, height:Number, value:String, pointCnt:Number)
	{
		var mc:MovieClip = _root.createEmptyMovieClip( "Tip_" + pointCnt, _root.getNextHighestDepth() );
		var gs:Graphics = new Graphics(mc);		
		var link:String = "" ;	//对应的超链接
		
		mc.ToolTip(value, link);
	
		gs.DrawRect(pen, x, y, width, height);
		gs.FillRect(brush, x, y, width, height);		
		
		this.mChildmcArr.push(mc);
	}	
	
    public function DrawData()
    {
       	var mc : MovieClip = mClip;
        var gs:Graphics = new Graphics(mc);		
		var pen = new Pen(1.0,0xcccccc,100);		
		var brush = new SolidBrush(0x000000, 100);
		var yTickCnt : Number = 0;
		var dateArray : Array;
		var valueArray : Array;
		var purposeArray : Array;
		var curGroupTop : Number = mChartTop ;
		var tmpX:Number = 0, lastTmpX:Number = 0;
		var tmpY:Number = 0;
		var lastTmpHeight = 0;
		var cnt:Number = 0;
		var pointCnt:Number = 0;
		var tmpName:String;
		var curDosage : Number=0, lastDosage:Number=0 ;
		var curTreatmentIdx=0;		
		var curPurpose = -1, lastPurpose = -1;
		var tmpTxt:String ;
		var bSkip = 0;
		var lastValidIdx = 0;
		
		for(var i=0; i < mGroupCount; i++)
	   	{
			tmpY = curGroupTop + mYGap;
			yTickCnt = Number(mGroup[i][1]);			
			for(var j=0; j < yTickCnt; j++, curTreatmentIdx++)	
			{
				dateArray = mData["Time_" + (i+1) + "_" + (j+1)].split(";");
				valueArray = mData["Value_" + (i+1) + "_" + (j+1)].split(";");				
				purposeArray = mData["Purpose_" + (i+1) + "_" + (j+1)].split(";");				
				
				tmpName = mTreatmentArr[curTreatmentIdx][0];				
				cnt = dateArray.length;
				if(cnt != valueArray.length || cnt != purposeArray.length)	//验证数据有效性
				{
					break;
				}		
				
				lastValidIdx = 1;
				for(var t=1; t <= valueArray.length; t++)
				{
					if(valueArray[t-1] > 0) lastValidIdx = t;
				}
				
				//重新初始化相关变量
				lastTmpX = 0;
				lastPurpose = -1;
				lastDosage = 0;
				bSkip = 0;
				
				for(var k=1; k<=cnt; k++)
				{		
					curDosage = valueArray[k-1];
					curPurpose = purposeArray[k-1];					
					
					//计算每个点对应的坐标值
					tmpX = GetXMigration(Utils.StrToDate(dateArray[k-1]));	
					if(tmpX > 0 && lastPurpose >= 0)
					{	
						//不是第一个有效的点则需要连线
						if(k > 1)	
						{
							//gs.DrawRect(pen, mChartLeft + lastTmpX, tmpY - lastTmpHeight/2.0, tmpX-lastTmpX, lastTmpHeight);
							//gs.FillRect(brush, mChartLeft + lastTmpX, tmpY - lastTmpHeight/2.0, tmpX-lastTmpX, lastTmpHeight);						
							tmpTxt = "从" + dateArray[k-2] + "\n到" + dateArray[k-1] + 
								"\n用量：" + lastDosage + mTreatmentArr[curTreatmentIdx][1];
							DrawRectWithTips(pen, brush, mChartLeft + lastTmpX, tmpY - lastTmpHeight/2.0, tmpX-lastTmpX, lastTmpHeight,
											 tmpTxt, pointCnt++);
							
							//输出该段记录所对应的剂量,还需要判断是否剂量发生了变化(即是否需要跳过)
							if(!bSkip)
							{
								tmpTxt = lastDosage + mTreatmentArr[curTreatmentIdx][1];
								if(k-1 == lastValidIdx)
								{
									DrawHtmlText(tmpTxt, mChartLeft + lastTmpX + lastTmpHeight, tmpY - 2.5*lastTmpHeight, mTitleColor, 10);
								}
							}							
							
							bSkip = lastDosage == curDosage ? 1 : 0;								
						}					
					}
					else	//需要取最后一个无效点
					{
						lastTmpX = 0;
					}						
						
					//更新画笔以及画刷
					if(curPurpose >= 0)
					{
						pen.setColor(mLegendOption[curPurpose][1]);
						brush.setColor(mLegendOption[curPurpose][1]);
						lastTmpHeight = mLegendOption[curPurpose][2];		
					}					
					lastPurpose = curPurpose;					
					
					//判断剂量是否发生变化,如果变化，则需要标识出变化					
					if(tmpX > 0)
					{
						if(lastDosage != curDosage && lastDosage > 0 && curDosage > 0)
						{
							tmpTxt = "剂量变化";
							DrawPoint(pen, brush, mChartLeft + tmpX - lastTmpHeight/2.0, tmpY - lastTmpHeight, lastTmpHeight, lastTmpHeight*1.5, tmpTxt, pointCnt++);
						}												
						lastTmpX = tmpX ;
					}
					
					lastDosage = curDosage;																	
				}				
				
				//判断最后一个点是否为最新更新时间，如果是则不需要再处理，如果不是，则需要按照上一个点的值来画延长线
				if(lastPurpose >= 0 && Utils.GetDayNumber(Utils.StrToDate(dateArray[cnt-1]), mUpdateDate) < 0)
				{
					//gs.DrawRect(pen, mChartLeft + lastTmpX, tmpY - lastTmpHeight/2.0, mChartWidth-lastTmpX, lastTmpHeight);
					//gs.FillRect(brush, mChartLeft + lastTmpX, tmpY - lastTmpHeight/2.0, mChartWidth-lastTmpX, lastTmpHeight);						
					tmpTxt = "从" + dateArray[cnt-1] + "\n至 今" + 
							"\n用量：" + lastDosage + mTreatmentArr[curTreatmentIdx][1];	
					DrawRectWithTips(pen, brush, mChartLeft + lastTmpX, tmpY - lastTmpHeight/2.0, mChartWidth-lastTmpX, lastTmpHeight,
											 tmpTxt, pointCnt++);					
					
					//输出该段记录所对应的剂量,还需要判断是否剂量发生了变化(即是否需要跳过)
					if(!bSkip)
					{
						tmpTxt = lastDosage + mTreatmentArr[curTreatmentIdx][1];
                        DrawHtmlText(tmpTxt, mChartLeft + lastTmpX + lastTmpHeight, tmpY - 2.5*lastTmpHeight, mTitleColor, 10);					

					}
				}
				
				tmpY += mYGap;				
			}
			
			curGroupTop = tmpY + mYGap ;
		}
    }  
}